<template>
  <header class="header">
    <div class="logo">
      <i class="fas fa-file-contract logo-icon"></i>
      <div class="logo-text">合规检验智能体</div>
    </div>
    <nav class="nav-links">
      <a 
        v-for="link in navLinks" 
        :key="link.name"
        href="#" 
        :class="['nav-link', { active: link.active }]"
        @click.prevent="handleNavClick(link)"
      >
        {{ link.name }}
      </a>
    </nav>
    <div class="user-info">
      <span class="username">{{ username }}</span>
      <button class="logout-btn" @click="handleLogout">退出</button>
    </div>
  </header>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useHeaderNavStore } from '../stores/headerNavStore'
import { authService } from '../scripts/auth/api'
import { authStore } from '../scripts/auth/store'
import type { NavLink } from '../types'

const router = useRouter()
const { navLinks, setActiveLink } = useHeaderNavStore()
const username = ref('')

onMounted(() => {
  // 从localStorage获取用户名
  username.value = localStorage.getItem('username') || '用户'
  
  // 根据当前路由路径设置正确的导航高亮状态
  const currentPath = router.currentRoute.value.path
  
  // 根据路径确定应该高亮哪个导航链接
  if (currentPath.includes('/home')) {
    setActiveLink('首页')
  } else if (currentPath.includes('/records')) {
    setActiveLink('检验记录')
  } else if (currentPath.includes('/rules')) {
    setActiveLink('规则管理')
  } else if (currentPath.includes('/settings')) {
    setActiveLink('设置')
  }
})

// 处理导航点击
const handleNavClick = (link: NavLink) => {
  const currentPath = router.currentRoute.value.path
  let targetPath = '/'
  
  switch (link.name) {
    case '首页':
      targetPath = '/home'
      break
    case '检验记录':
      targetPath = '/records'
      break
    case '规则管理':
      targetPath = '/rules'
      break
    case '设置':
      targetPath = '/settings'
      break
  }

  // 只有在当前不是目标路径时才跳转
  if (currentPath !== targetPath) {
    router.push(targetPath).then(() => {
      setActiveLink(link.name)
    })
  } else {
    setActiveLink(link.name)
  }
}

// 处理退出登录
const handleLogout = async () => {
  try {
    // 调用登出API
    await authService.logout()
    
    // 清除认证状态（包括cookie中的token和localStorage中的其他信息）
    authStore.clearAuth()
    
    // 重定向到登录页面
    router.push('/')
  } catch (error) {
    console.error('登出失败:', error)
    alert('登出失败，请稍后重试')
  }
}
</script>

<style scoped>
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 30px;
  height: 70px;
  background: linear-gradient(135deg, var(--primary) 0%, var(--secondary) 100%);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  position: relative;
  z-index: 100;
}

.logo {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo-icon {
  font-size: 28px;
  color: white;
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.2));
}

.logo-text {
  font-size: 20px;
  font-weight: 600;
  color: white;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.nav-links {
  display: flex;
  gap: 15px;
}

.nav-link {
  text-decoration: none;
  color: rgba(255, 255, 255, 0.9);
  font-weight: 500;
  padding: 8px 16px;
  border-radius: 8px;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 8px;
}

.nav-link:hover {
  color: white;
  background-color: rgba(255, 255, 255, 0.15);
}

.nav-link.active {
  color: white;
  background-color: rgba(255, 255, 255, 0.25);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.user-info {
  display: flex;
  align-items: center;
  gap: 15px;
}

.username {
  font-weight: 500;
  color: white;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

.logout-btn {
  background-color: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 8px;
  padding: 8px 16px;
  cursor: pointer;
  transition: all 0.3s ease;
  color: white;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 8px;
}

.logout-btn:hover {
  background-color: rgba(255, 255, 255, 0.2);
  border-color: rgba(255, 255, 255, 0.5);
  transform: translateY(-1px);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header {
    padding: 0 15px;
    height: 60px;
  }
  
  .logo-text {
    font-size: 18px;
  }
  
  .logo-icon {
    font-size: 24px;
  }
  
  .nav-link, .logout-btn {
    padding: 6px 12px;
    font-size: 14px;
  }
}

@media (max-width: 480px) {
  .logo-text {
    display: none;
  }
  
  .username {
    display: none;
  }
  
  .nav-links {
    gap: 8px;
  }
}
</style>